//
//  AccountBindingVC.m
//  SelfDriving
//
//  Created by apple on 16/1/11.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "AccountBindingVC.h"
#import <Contacts/Contacts.h>
#import <ContactsUI/ContactsUI.h>
#import <AddressBook/AddressBook.h>
#import "AccountBindingSVC.h"
@interface AccountBindingVC ()
@property (weak, nonatomic) IBOutlet UITableView *table;
@end

@implementation AccountBindingVC
{
    int i;
}
- (void)viewDidLoad {
    [super viewDidLoad];
}
-(void)viewWillAppear:(BOOL)animated{
    [_table reloadData];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    if (indexPath.row == 0) {
        cell.textLabel.text = @"新浪微博";
        BOOL isOauth = YES;
        if ([[AppDelegate shareDelegate].user.bind_sina isEqualToString:@"1"]) {
            isOauth = YES;
        }
        else {
            isOauth = NO;
        }
        cell.detailTextLabel.text = isOauth ? @"已绑定" : @"未绑定";
        cell.imageView.image = isOauth ? [UIImage imageNamed:@"图标---微信"] : [UIImage imageNamed:@"图标未点亮---微信"];
//        if ([[AppDelegate shareDelegate].user.bind_sina isEqualToString:@"1"]) {
//            cell.detailTextLabel.text = @"已绑定";
//            cell.imageView.image = [UIImage imageNamed:@"图标---微博"];
//        }
//        else {
//            cell.detailTextLabel.text = @"未绑定";
//            cell.imageView.image = [UIImage imageNamed:@"图标未点亮---微博"];
//        }
        
    }
    if (indexPath.row == 1) {
        cell.textLabel.text = @"腾讯QQ";
        
        
        
        if ([[AppDelegate shareDelegate].user.bind_qq isEqualToString:@"1"]) {
            cell.detailTextLabel.text = @"已绑定";
            cell.imageView.image = [UIImage imageNamed:@"图标---qq"];
        }
        else {
            cell.detailTextLabel.text = @"未绑定";
            cell.imageView.image = [UIImage imageNamed:@"图标未点亮---qq"];
        }
        
    }
    if (indexPath.row == 2) {
        cell.textLabel.text = @"微信";
        if ([[AppDelegate shareDelegate].user.bind_weixin isEqualToString:@"1"]) {
            cell.detailTextLabel.text = @"已绑定";
            cell.imageView.image = [UIImage imageNamed:@"图标---微信"];
        }
        else {
            cell.detailTextLabel.text = @"未绑定";
            cell.imageView.image = [UIImage imageNamed:@"图标未点亮---微信"];
        }

    }

    if (indexPath.row == 3) {
        cell.textLabel.text = @"手机通讯录";
        cell.imageView.image = [UIImage imageNamed:@"sjzh"];
        //新建一个通讯录类
        ABAddressBookRef addressBooks = nil;
        if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0)
            
        {
            addressBooks =  ABAddressBookCreateWithOptions(NULL, NULL);
            //获取通讯录权限
            dispatch_semaphore_t sema = dispatch_semaphore_create(0);
            ABAddressBookRequestAccessWithCompletion(addressBooks, ^(bool granted, CFErrorRef error){dispatch_semaphore_signal(sema);});
            dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
        }
        else
        {
            addressBooks = ABAddressBookCreate();
        }
        
//         CFIndex nPeople = ABAddressBookGetPersonCount(addressBooks);
         cell.detailTextLabel.text = [[AppDelegate shareDelegate].txl isEqualToString:@"绑定"]?@"已绑定":@"未绑定";
    }
    
    
    
        return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row  == 0) {
        UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"GR"
                                                                     bundle:[NSBundle mainBundle]];
        AccountBindingSVC *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"AccountBindingSVC"];
        searchResultDetailController.type = @"新浪";
        [self.navigationController pushViewController:searchResultDetailController animated:YES];
    }
    if (indexPath.row  == 1) {
        UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"GR"
                                                                     bundle:[NSBundle mainBundle]];
        AccountBindingSVC *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"AccountBindingSVC"];
        searchResultDetailController.type = @"QQ";
        [self.navigationController pushViewController:searchResultDetailController animated:YES];
    }
    if (indexPath.row  == 2) {
        UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"GR"
                                                                     bundle:[NSBundle mainBundle]];
        AccountBindingSVC *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"AccountBindingSVC"];
        searchResultDetailController.type = @"微信";
        [self.navigationController pushViewController:searchResultDetailController animated:YES];
    }
    
    if (indexPath.row  == 3) {
        UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"GR"
                                                                     bundle:[NSBundle mainBundle]];
        AccountBindingSVC *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"AccountBindingSVC"];
        searchResultDetailController.type = @"通讯录";
        [self.navigationController pushViewController:searchResultDetailController animated:YES];
    }
    
}

- (IBAction)backActino:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


@end
